/*              Copyright(c) 2008 Dave Sexton              *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DaveSexton.DocToMaml
{
	internal sealed partial class OptionsDialog : Form
	{
		#region Public Properties
		#endregion

		#region Private / Protected
		private readonly ProgramSettings settings;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="OptionsDialog" /> class.
		/// </summary>
		public OptionsDialog(ProgramSettings settings)
			: this()
		{
			if (settings == null)
				throw new ArgumentNullException("settings");

			this.settings = settings;

			LoadSettings();
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="OptionsDialog" /> class.
		/// </summary>
		private OptionsDialog()
		{
			InitializeComponent();
		}
		#endregion

		#region Methods
		private void LoadSettings()
		{
			switch (settings.Startup)
			{
				case StartupProject.NewProject:
					startupNewProjectRadioButton.Checked = true;
					break;
				case StartupProject.OpenLastProject:
					startupLastProjectRadioButton.Checked = true;
					break;
				case StartupProject.OpenProject:
					startupOpenProjectRadioButton.Checked = true;
					break;
			}

			minimizeToTrayCheckBox.Checked = settings.MinimizeToTray;
			minimizeOnCloseCheckBox.Checked = settings.MinimizeOnClose;
			projectDirectoryTextBox.Text = settings.ProjectDirectory;
		}

		private void CommitSettings()
		{
			if (startupLastProjectRadioButton.Checked)
				settings.Startup = StartupProject.OpenLastProject;
			else if (startupNewProjectRadioButton.Checked)
				settings.Startup = StartupProject.NewProject;
			else if (startupOpenProjectRadioButton.Checked)
				settings.Startup = StartupProject.OpenProject;

			settings.MinimizeToTray = minimizeToTrayCheckBox.Checked;
			settings.MinimizeOnClose = minimizeOnCloseCheckBox.Checked;
			settings.ProjectDirectory = projectDirectoryTextBox.Text;
		}
		#endregion

		#region Event Handlers
		private void okButton_Click(object sender, EventArgs e)
		{
			CommitSettings();

			DialogResult = DialogResult.OK;
		}

		private void startupOpenProjectRadioButton_CheckedChanged(object sender, EventArgs e)
		{
			projectDirectoryTextBox.Enabled = startupOpenProjectRadioButton.Checked;
		}

		private void projectDirectoryBrowseButton_Click(object sender, EventArgs e)
		{
			using (FolderBrowserDialog dialog = new FolderBrowserDialog())
			{
				dialog.Description = Resources.Text.BrowseDefaultOpenProjectFolderDescription;
				dialog.RootFolder = Environment.SpecialFolder.MyDocuments;
				dialog.ShowNewFolderButton = true;

				if (dialog.ShowDialog(this) == DialogResult.OK)
					projectDirectoryTextBox.Text = dialog.SelectedPath;
			}
		}

		private void minimizeToTrayCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			minimizeOnCloseCheckBox.Enabled = minimizeToTrayCheckBox.Checked;
		}
		#endregion
	}
}